
'''

  Bonree 监控入口程序


'''

import time
import threading

from spider import cray_shanghai, cray_beijin, cray_shenzhen
from sqlq import SqlTest
from mail import send_email


# 获取数据库数据
def _get_sql_data():

  # 数据库账号获取
  st = SqlTest()
  account_list, imsi_list = st.testCase()
  account_list_str = "无"
  imsi_list_str = "无"
  if(len(account_list) > 0):
    account_list_str = ";".join(account_list)

  if(len(imsi_list) > 0):
    imsi_list_str = ""

  return account_list_str, imsi_list_str



# 获取排名，生成报告
def _get_report():
  
  list_sz, rank_sz = cray_shenzhen(True)
  list_sh, rank_sh = cray_shanghai(True)
  list_bj, rank_bj = cray_beijin(True)

  account_list_str, imsi_list_str = _get_sql_data()

  param = {}
  param["list_sz"] = list_sz
  param["list_sh"] = list_sh
  param["list_bj"] = list_bj
  param["account_list_str"] = account_list_str
  param["imsi_list_str"] = imsi_list_str
  param["rank_sz"] = rank_sz
  param["rank_sh"] = rank_sh
  param["rank_bj"] = rank_bj
  send_email(True, param)


# 数据监控
def _get_site_data():
  
  score_sz, rank_sz = cray_shenzhen(False)
  score_sh, rank_sh = cray_shanghai(False)
  score_bj, rank_bj = cray_beijin(False)
  account_list_str, imsi_list_str = _get_sql_data()

  param = {}
  param["score_bj"] = score_bj
  param["score_sz"] = score_sz
  param["score_sh"] = score_sh
  param["rank_bj"] = rank_bj
  param["rank_sh"] = rank_sh
  param["rank_sz"] = rank_sz
  param["account_list_str"] = account_list_str
  param["imsi_list_str"] = imsi_list_str

  # score = -1，表示 session 过期了，需要重新获取 session
  if(rank_sz == -1):
    print("session id 失效")
    send_email(False, param)
    return

  '''
    低于592，发邮件通知
    测试有3个时间点，9:30，10：00，13:00，每批最高分数 200
    所以通过判断来区分第一批，还是第二批

    第一批 200 > score > 197
    第二批 400 > score > 394
    第三批 600 > score > 592

  '''
  s_score_low_1 = 197
  s_score_high_1 = 200
  s_score_low_2 = 394
  s_score_high_2 = 400
  s_score_low_3 = 592
  s_score_high_3 = 600
  is_err = True
  if(score_sz < s_score_high_1 and score_sz > s_score_low_1 and score_sh < s_score_high_1 and score_sh > s_score_low_1 and score_bj < s_score_high_1 and score_bj > s_score_low_1):
    is_err = False

  if(score_sz < s_score_high_2 and score_sz > s_score_low_2 and score_sh < s_score_high_2 and score_sh > s_score_low_2 and score_bj < s_score_high_2 and score_bj > s_score_low_2):
    is_err = False

  if(score_sz < s_score_high_3 and score_sz > s_score_low_3 and score_sh < s_score_high_3 and score_sh > s_score_low_3 and score_bj < s_score_high_3 and score_bj > s_score_low_3):
    is_err = False

  '''
    是否是 9:30 之前
    
    如果是上午未开市前
    长江的 score_sz, score_sh, score_bj 为0
    rank_sz, rank_sh, rank_bj 为 31
  '''
  is_a930 = True
  if(score_bj == 0 and score_sh == 0 and score_sz == 0 and rank_bj == 31 and rank_sh == 31 and rank_sz == 31):
    is_a930 = False

  if((rank_bj > 8 or rank_sh > 8 or rank_sz > 8 or account_list_str != "无" or imsi_list_str != "无" ) and is_a930 and is_err):
    send_email(False, param)



def runSpy():

  print("Timer: %s" % time.strftime("%Y%m%d %X"))

  '''
    判断是否需要查询博睿数据
    
    监控交易时间段，9:30 - 9:35， 10:00 - 10.05， 13:00 - 13:05

    报告时间段
  '''
  tt = time.strftime("%X")
  if(tt < "09:30" or (tt > "09:35" and tt < "10:00") or (tt > "10:05" and tt < "13:00")):
    timer = threading.Timer(300, runSpy)
    timer.start()
    return

  # 下班时间判断
  is_offwork = time.strftime("%X") > "13:06"
  if(is_offwork):
    _get_report()
    print("report")
  else:
    _get_site_data()
    print("spy")
    timer = threading.Timer(300, runSpy)
    timer.start()


if __name__ == "__main__":
  runSpy()